package com.gaofei.controller;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gaofei.domain.Brand;
import com.gaofei.domain.Spu;
import com.gaofei.domain.SpuVo;
import com.gaofei.service.BrandService;
import com.gaofei.service.SpuService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("spu")
public class SpuController {

	@Reference
	SpuService spuService;
	
	@Reference
	BrandService brandService;
	
	@Value("${upload.path}")
	private String fileUploadPath;
	
	
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Spu spu,MultipartFile picFileName){
		try {
			System.err.println(spu);
			//开始上传
			String fileUpload = fileUpload(picFileName);
			//System.err.println(fileUpload);
			if(fileUpload!=null && fileUpload!="") {
				spu.setSmallPic(fileUpload);
			}
			spuService.update(spu);
			return true;
			
		} catch (Exception e) {
			return false;
		}
		
	}
	
	
	@RequestMapping("toUpdate/{spuId}")
	public String toUpdate(Model m,@PathVariable("spuId")Integer spuId){
		//根据spuId查询spu
		Spu spu = spuService.findSpuById(spuId);
		System.out.println(spu);
		m.addAttribute("spu", spu);
		
		PageInfo<Brand> brandInfo = brandService.findAll(1, Integer.MAX_VALUE);
		m.addAttribute("brands", brandInfo.getList());
		return "spu/update";
	}
	
	
	@RequestMapping("add")
	@ResponseBody
	public boolean add(Spu spu,MultipartFile picFileName){
		try {
			//开始上传
			String fileUpload = fileUpload(picFileName);
			spu.setSmallPic(fileUpload);
			spuService.addSpu(spu);
			return true;
			
		} catch (Exception e) {
			return false;
		}
		
	}
	
	//	文件上传的方法
	public String fileUpload(MultipartFile picFileName) {
		String filename = "";
		if(picFileName!=null && !picFileName.isEmpty()) {
			String originalFilename = picFileName.getOriginalFilename();
			filename =UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
			try {
				picFileName.transferTo(new File(fileUploadPath, filename));
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		return filename;
	}
	
	
	
	@RequestMapping("toAdd")
	public String toAdd(Model m) {
		PageInfo<Brand> brandInfo = brandService.findAll(1, Integer.MAX_VALUE);
		m.addAttribute("brands", brandInfo.getList());
		return "spu/add";
	}
	
	@RequestMapping("delBatch")
	@ResponseBody
	public boolean delBatch(@RequestParam("ids[]")Integer [] ids) {
		try {
			
			spuService.delBatch(ids);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	@RequestMapping("list")
	public String list(Model m,SpuVo vo ,@RequestParam(defaultValue="1")int pageNum ,@RequestParam(defaultValue="5")int pageSize) {
		PageInfo<Spu> info = spuService.list(pageNum, pageSize, vo);
		m.addAttribute("info", info);
		m.addAttribute("vo", vo);
		PageInfo<Brand> brandInfo = brandService.findAll(1, Integer.MAX_VALUE);
		m.addAttribute("brands", brandInfo.getList());
		return "spu/list";
	}
}
